﻿using System;
using System.Windows;
using System.Windows.Data;
using Pixster.Slideshow.BusinessObjects;

namespace Pixster.Slideshow
{
    public partial class MainPage
    {
        private readonly ServiceFacade _facade;

        public MainPage()
        {
            InitializeComponent();
            _facade = new ServiceFacade();
            _facade.Loaded += OnFacadeLoaded;
            _facade.Load();
            this.IsLoading = true;
        }

        private void OnFacadeLoaded(object sender, EventArgs e) {

            var control = new SlideShowControl {
                                                   Name = "slideShowControl",
                                                   HorizontalAlignment = HorizontalAlignment.Center,
                                                   VerticalAlignment = VerticalAlignment.Center,
                                                   HorizontalContentAlignment = HorizontalAlignment.Stretch,
                                                   VerticalContentAlignment = VerticalAlignment.Stretch,
                                                   Style = (Style)Application.Current.Resources["SlideShowControl"]
                                               };

            var binding = new Binding {Source = this._facade.CurrentSlideShow};
            control.SetBinding(SlideShowControl.PhotoSlideShowProperty, binding);

            this.IsLoading = false;
            this.LayoutRoot.Children.Add(control);
            control.ResumeSlideShow();
        }

        private bool IsLoading
        {
            set
            {
                if (value)
                {
                    this.LoadingAnim.Animiation.Begin();
                    this.LoadingAnim.Visibility = Visibility.Visible;
                }
                else
                {
                    this.LoadingAnim.Animiation.Stop();
                    this.LoadingAnim.Visibility = Visibility.Collapsed;
                }
            }
        }
    }
}